package com.fancy.config;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;


@Component
public class LoginFilter extends ZuulFilter {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();
        String LOGIN_URL = "/fancy-music-login/user/login";
        String REGISTRY = "/fancy-music-login/user/registry";
        String SEND_MAIL = "/fancy-music-login/user/sendMail";
        if (requestURI.equals(LOGIN_URL)||requestURI.equals(SEND_MAIL)||requestURI.equals(REGISTRY)){
            return false;
        }
        return true;

    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        Cookie[] cookies = request.getCookies();

        String token = getToken(cookies);

        if (token == null) {
            currentContext.setSendZuulResponse(false);
            HttpServletResponse response = currentContext.getResponse();
            response.setContentType("application/json;charset=utf-8");

            currentContext.setResponseBody("aaaaaaaaaa用户未登录");
            currentContext.setResponse(response);
        }


        if (token != null && !redisTemplate.hasKey(token)) {
            currentContext.setSendZuulResponse(false);
            HttpServletResponse response = currentContext.getResponse();
            response.setContentType("application/json;charset=utf-8");

            currentContext.setResponseBody("token失效，请重新登录");
        }

        if(token!=null && redisTemplate.hasKey(token)){
            redisTemplate.expire(token,3000, TimeUnit.SECONDS);
        }


        return null;
    }

    private String getToken(Cookie[] cookies) {
        if (cookies != null) {
            for (Cookie cookie :
                    cookies) {
                if (cookie.getName().equals("token")) {
                    String value = cookie.getValue();
                    return value;
                }
                return null;
            }
        }
        return null;
    }
}
